home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dutil / Join.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  1KB  |  73 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  JOIN.C
  9.  *
  10.  *  JOIN files AS outfile
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <fcntl.h>
  16. #ifdef AMIGA
  17. #include <libc.h>
  18. #else
  19. #include <unistd.h>
  20. #endif
  21.  
  22. char Buf[65536];
  23.  
  24. int
  25. main(int ac, char **av)
  26. {
  27.     int i;
  28.     int fdo;
  29.     int error = 0;
  30.     char *outFile = NULL;
  31.     char *ptr;
  32.  
  33.     for (i = 1; i < ac; ++i) {
  34.     ptr = av[i];
  35.  
  36.     if (strcmp(ptr, "AS") == 0 || strcmp(ptr, "as") == 0) {
  37.         outFile = av[i + 1];
  38.         av[i] = NULL;
  39.         break;
  40.     }
  41.     }
  42.     if (outFile == NULL) {
  43.     fprintf(stderr, "No AS option specified\n");
  44.     exit(20);
  45.     }
  46.     if ((fdo = open(outFile, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
  47.     fprintf(stderr, "couldn't create %s\n", outFile);
  48.     exit(20);
  49.     }
  50.     for (i = 1; error == 0 && (ptr = av[i]); ++i) {
  51.     int fd;
  52.     long n;
  53.  
  54.     if ((fd = open(ptr, O_RDONLY)) < 0) {
  55.         fprintf(stderr, "couldn't open %s\n", ptr);
  56.         error = 20;
  57.     } else {
  58.         while ((n = read(fd, Buf, sizeof(Buf))) > 0) {
  59.         if (write(fdo, Buf, n) != n) {
  60.             fprintf(stderr, "write error\n");
  61.             error = 20;
  62.             break;
  63.         }
  64.         }
  65.     }
  66.     }
  67.     close(fdo);
  68.     if (error)
  69.     unlink(outFile);
  70.     exit(error);
  71. }
  72.  
  73.